<template>
  <div>
    <el-card shadow="never" style="border: none">
      <div slot="header">
        <div class="header_title">
          <span>关联巡查任务</span>
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >关联</el-button>
        </div>
      </div>

      <el-table v-loading="loading" :data="itemList">
        <el-table-column label="任务名称" align="center" prop="name" />
        <el-table-column label="处理人" align="center" prop="modifiedMethod" />
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_task_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" prop="updateTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-tickets"
              @click="goDetailPage(scope.row)"
            >详情</el-button>
            <el-button
              v-hasPermi="['system:item:remove']"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNo"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="关联巡查任务" prop="taskId">
          <el-select
            v-model="form.taskId"
            filterable
            remote
            placeholder="请输入关键词查询"
            :remote-method="searchTask"
            :loading="searchLoading"
          >
            <el-option
              v-for="item in taskList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listRelation, delRelation, addRelation } from '@/api/accident/item'
import { listTask } from '@/api/task/task'
export default {
  name: 'EventRelatedTask',
  dicts: ['sys_business_type', 'sys_task_status'],
  props: {
    accId: {
      type: String,
      required: true
    },
    corpId: {
      type: String,
      default: null
    },
    flush: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      total: 0,
      // 【请填写功能名称】表格数据
      itemList: [],
      title: '',
      // 是否显示弹出层
      open: false,
      form: {},
      searchLoading: false,
      // 表单校验
      rules: {
      },
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        accId: null
      },
      taskQuery: {
        pageNo: 1,
        pageSize: 1000,
        corpId: null,
        status: 2
      },
      taskList: []
    }
  },
  watch: {
    flush() {
      this.getList()
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      this.queryParams.accId = this.accId
      listRelation(this.queryParams).then(response => {
        this.itemList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        accId: this.accId,
        status: null,
        description: null,
        remark: null,
        type: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        modifyBy: null,
        modifiedMethod: null,
        taskId: null
      }
      this.resetForm('form')
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加处理记录'
    },

    searchTask(query) {
      const params = {
        pageNo: 1,
        pageSize: 10,
        name: query,
        corpId: this.corpId,
        status: 2
      }
      this.searchLoading = true
      listTask(params).then(response => {
        const data = response.data.records.map(item => ({
          ...item
        }))
        this.taskList = data
        this.searchLoading = false
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.accId = this.accId
          addRelation(this.form).then(response => {
            this.$modal.msgSuccess('新增成功')
            this.open = false
            this.getList()
            this.$emit('data-flush')
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除处理记录编号为"' + ids + '"的数据项？').then(function() {
        return delRelation(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
        this.$emit('data-flush')
      }).catch(() => {})
    },
    goDetailPage(row) {
      this.$router.push({
        name: 'TaskDetail',
        params: {
          taskId: row.id,
          type: row.type || 'environment'
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.header_title{
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
</style>

